const CacheType = {
	Local: 'Local',
	Session: 'Session'
}

class Cache {
	storage

	constructor(type) {
		this.storage = type === CacheType.Local ? window.localStorage : window.sessionStorage
	}

	setCache(key, value) {
		if (value) {
			this.storage.setItem(key, JSON.stringify(value))
		}
	}

	getCache(key) {
		const value = this.storage.getItem(key)
		if (value) {
			const obj = JSON.parse(value)
			if (typeof obj === 'string') {
				return JSON.parse(obj)
			}
			return obj
		}
		return {}
	}

	removeCache(key) {
		this.storage.removeItem(key)
	}

	clear() {
		this.storage.clear()
	}
}

const localCache = new Cache(CacheType.Local)
const sessionCache = new Cache(CacheType.Session)

export { localCache, sessionCache }
